package cn.cloudplug.ui.widget;

import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.view.ViewGroup;

import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;

import java.util.LinkedHashMap;
import java.util.List;

import cn.cloudplug.core.index.ConfigKeys;
import cn.cloudplug.core.index.Scent;
import cn.cloudplug.core.util.MyUtil;
import cn.cloudplug.core.util.log.LogUtil;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by pc on 2018/2/2.
 */

public class SuspensionButton implements FloatingActionMenu.MenuStateChangeListener,View.OnClickListener {
    private final static String TAG="SuspensionButton";
    private static final int RADIUS = 5;
    private final static int WIDTH=100;
    private final static int HEIGHT=100;
    private final static int MARGIN_BOTTOM=25;
    private final static int MARGIN_RIGHT=25;
    private static final int ITEM_HEIGHT = 97;
    private static final int ITEM_WIDTH = 97;
    private final FloatingActionMenu menu;
    private CircleImageView mainButton;
    private AppCompatImageView susBg;
    private static final int MENU_RADIUS = 353;
    private MenuStateChangeListener menuStateChangeListener = null;
    private onClickListener onClickListener=null;
    private AppCompatActivity activity;
    private List<SuspensionButton.Bean> buttons;
    private ViewGroup rootGroup;
    private LinkedHashMap<AppCompatImageView,RotateTextView> imageViewTextViewLinkedHashMap=new LinkedHashMap<>();
    private boolean isInitTextView=false;

    public SuspensionButton(AppCompatActivity activity, List<SuspensionButton.Bean> buttons) {
        this.activity = activity;
        this.buttons = buttons;
        this.rootGroup = activity.getWindow().getDecorView().findViewById(android.R.id.content);

        mainButton = new CircleImageView(activity);
        mainButton.setBackground(null);
        susBg = new AppCompatImageView(activity);
        susBg.setBackgroundColor(0x88ffffff);
        susBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menu.close(true);
            }
        });

        int[] screenSize = Scent.getConfiguration(ConfigKeys.ScreenSize);
        mainButton.setTranslationY(screenSize[1] - HEIGHT - MARGIN_BOTTOM);
        mainButton.setTranslationX(screenSize[0] - WIDTH - MARGIN_RIGHT);

        FloatingActionMenu.Builder builder = new FloatingActionMenu.Builder(activity)
                .setStartAngle(180)
                .setEndAngle(270)
                .setRadius(MENU_RADIUS)
                // listen state changes of each menu
                .setStateChangeListener(this)
                .attachTo(mainButton);

        rootGroup.addView(susBg, new ViewGroup.LayoutParams(screenSize[0], screenSize[1]));
        rootGroup.addView(mainButton, new ViewGroup.LayoutParams(WIDTH, HEIGHT));

        if (buttons != null)
            for (Bean button : buttons) {
                AppCompatImageView imageView = new AppCompatImageView(activity);
                imageView.setImageResource(button.getImgId());
                RotateTextView textView = new RotateTextView(activity);
                textView.setText(button.getTitle());
                rootGroup.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                textView.setTextSize(14);
                textView.setVisibility(View.GONE);
                imageViewTextViewLinkedHashMap.put(imageView, textView);
                builder.addSubActionView(imageView, ITEM_WIDTH, ITEM_HEIGHT);
                imageView.setOnClickListener(button.getOnClickListener());
                imageView.setId(button.getId());
            }
        menu = builder.build();

        mainButton.setOnClickListener(this);

        mainButton.setVisibility(View.GONE);
        susBg.setVisibility(View.GONE);
    }

    public SuspensionButton.onClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(SuspensionButton.onClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public MenuStateChangeListener getMenuStateChangeListener() {
        return menuStateChangeListener;
    }

    public void setMenuStateChangeListener(MenuStateChangeListener menuStateChangeListener) {
        this.menuStateChangeListener = menuStateChangeListener;
    }

    public void showMainButtonToActivity() {
        mainButton.setVisibility(View.VISIBLE);
    }

    public void hideMainButtonToActivity() {
        mainButton.setVisibility(View.GONE);
    }

    private void showBg(){
        if(susBg.getVisibility()==View.VISIBLE)return;
        susBg.setVisibility(View.VISIBLE);
    }
    private void hideBg(){
        if(susBg.getVisibility()==View.GONE)return;
        susBg.setVisibility(View.GONE);
    }

    public void show(){
        showMainButtonToActivity();
        if(menu.isOpen()){
            showBg();
        }else{
            hideBg();
        }
    }

    public void setImage(int image) {
        mainButton.setImageResource(image);
    }

    @Override
    public void onMenuOpened(final FloatingActionMenu menu) {
        showBg();
        if(menuStateChangeListener!=null)menuStateChangeListener.onMenuOpened();
        Scent.mianHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!menu.isOpen()){
                    return;
                }
                List<FloatingActionMenu.Item> items = menu.getSubActionItems();
                if(items!=null)
                    for (FloatingActionMenu.Item item : items) {
                        RotateTextView rotateTextView = imageViewTextViewLinkedHashMap.get(item.view);
                        rotateTextView.setVisibility(View.VISIBLE);
                        if(!isInitTextView) {
                            Point center = MyUtil.getActionViewCenter(activity, item.view, false);
                            float[] coords = new float[]{0f, 0f};
                            MyUtil.positionByAngle(center, ITEM_WIDTH , item.rAngle, coords);
                            MyUtil.viewMeasure(rotateTextView);
                            rotateTextView.setX(coords[0]-rotateTextView.getMeasuredWidth()/2);
                            rotateTextView.setY(coords[1]-rotateTextView.getMeasuredHeight()/2);
                            rotateTextView.setRotation(item.rAngle - 90);
                        }
                        LogUtil.e("width",rotateTextView.getMeasuredWidth()+"");
                    }
                isInitTextView=true;
            }
        }, 1000);
    }

    @Override
    public void onMenuClosed(FloatingActionMenu menu) {
        hideBg();
        if(menuStateChangeListener!=null)menuStateChangeListener.onMenuClosed();
        List<FloatingActionMenu.Item> items = menu.getSubActionItems();
        if(items!=null)
            for (FloatingActionMenu.Item item : items) {
                RotateTextView rotateTextView = imageViewTextViewLinkedHashMap.get(item.view);
                rotateTextView.setVisibility(View.GONE);
            }
    }

    @Override
    public void onClick(View v) {
        if(onClickListener==null||(menu.isOpen()&&onClickListener.onClick()==false)){
            if(menu.isOpen()){
                menu.close(true);
            }else{
                menu.open(true);
            }
        }

    }

    public void hide() {
        hideMainButtonToActivity();
        hideBg();
    }

    public void close() {
        menu.close(true);
    }

    public static class Bean{
        private String title;
        private int imgId;
        private View.OnClickListener onClickListener;
        private int id;

        public View.OnClickListener getOnClickListener() {
            return onClickListener;
        }

        public void setOnClickListener(View.OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }

        public Bean(String title, int imgId, View.OnClickListener onClickListener, int id) {
            this.title = title;
            this.imgId = imgId;
            this.onClickListener = onClickListener;
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getImgId() {
            return imgId;
        }

        public void setImgId(int imgId) {
            this.imgId = imgId;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }

    public interface MenuStateChangeListener{
        void onMenuOpened();
        void onMenuClosed();
    }
    public interface onClickListener{
        boolean onClick();
    }

}
